﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ExtBusiness;
using WF_DataAccess;
public partial class SystemManager_DepartmentEditor : System.Web.UI.Page
{
    #region 窗体加载事件
    protected void Page_Load(object sender, EventArgs e)
    {
        /****单击部门时，获取该部门的信息****/
        if (!Page.IsPostBack)
        {
            ExtBusiness.Admin.Department deptOperation = new ExtBusiness.Admin.Department();
            DataTable dtlist = deptOperation.GetDepartment();
            
            if (dtlist.Rows.Count > 0)
            {
                DataRow dr = dtlist.NewRow();
                dr["departid"] = "0";
                dr["depart_Name"] = "无";
                dtlist.Rows.InsertAt(dr,0);
                sjbmlist.DataSource = dtlist;
                sjbmlist.DataTextField = "depart_name";
                sjbmlist.DataValueField = "departid";
                sjbmlist.DataBind();             
            }
            if (Request["depName"] != "" && Request["departId"] != "" && Request["orderId"] != "")
            {
                this.bmmc.Text = Server.UrlDecode(Request["depName"]);
                this.bmid.Text = Request["departId"];
                orderId.Text = Request["orderId"];
                string sjbm = string.Empty;
                DataTable dt = deptOperation.SearchDepartment(Request["departId"]);
                if (dt.Rows.Count > 0)
                {
                    sjbmlist.SelectedValue = dt.Rows[0][0].ToString();                    
                }          
            }

        }

    }
    #endregion

    #region 插入新部门
    protected void inputdata_Click(object sender, EventArgs e)
    {
        //清空原有数据
        bmid.Text = "";
        bmmc.Text = "";
        orderId.Text = "";
    }
    #endregion    

    #region 编辑部门信息
    protected void editdata_Click(object sender, EventArgs e)
    {
        string sjbm = sjbmlist.SelectedValue;//上级部门
        
        ExtBusiness.User.DepartHandle Department = new ExtBusiness.User.DepartHandle();
        string orderId = Department.GetMaxOrderId();
        if (String.IsNullOrEmpty(bmid.Text))//新增
        {
            string departId=Department.GetMaxDepart();
           
            Department.AddDepartment(bmmc.Text, sjbmlist.SelectedValue, orderId);
            msgAppear.Text = "新增成功";
            bmid.Text = departId; //用新ID赋值
        }
        else//修改
        {
            Department.UpdateDepartment(bmmc.Text, sjbm, orderId, Request["departid"]);
            msgAppear.Text = "修改成功";
        }   
    }
    #endregion

    #region 删除部门信息
    protected void deldata_Click(object sender, EventArgs e)
    {
        ExtBusiness.User.DepartHandle Department = new ExtBusiness.User.DepartHandle();
        int i = Department.DeleteDepartment(bmid.Text);
        
        if (i > 0) 
        msgAppear.Text = "删除成功";
        else 
           msgAppear.Text = "删除失败";
    }
    #endregion

    #region 获取新插入部门的上级部的ID号和该部门的order_id
   
    public string GetOrderId()
    {
        string id = bmid.Text;//部门ID
        ExtBusiness.User.DepartHandle Department = new ExtBusiness.User.DepartHandle();
        string departid = Department.GetDepartId(id);
        if (!String.IsNullOrEmpty(id) && sjbmlist.SelectedValue.Trim() == departid)//如果上级部门没有变，则OrderID也不变
        {
            return orderId.Text;
        }
        else//新增时或上级部门发生了变化
        {
            string orderId = "";
            orderId=Department.GetTopDepart(sjbmlist.SelectedValue);
            return orderId;
        }      
       
    }
    #endregion

       
    #region 对部门的人员进行重新设置
    protected void submit_Click(object sender, EventArgs e)
    {
        string[] selecteduser = Request.Params.GetValues("check");
        string[] sortID = txtOrderByID.Text.Split(',');

        ExtBusiness.User.DepartHandle.ResetDepartment(orderId.Text, selecteduser, sortID, bmid.Text);
        msgAppear.Text = "部门人员设置成功";

    }
    #endregion
}
